package com.cs2340.taskplusplus;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.os.Bundle;
import com.google.android.maps.*;

public class MapViewDisplay extends MapActivity{

	private MapController mc;
	private int lat, lon;
	private MapView mapView;
	
	
	@Override
	protected boolean isRouteDisplayed() 
	{
		return false;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.mapview);
	    mapView = (MapView) findViewById(R.id.mapview);
	    mapView.setBuiltInZoomControls(true);
	    
	    
		getSystemService(Context.LOCATION_SERVICE);
	    
		lat = CurrentLocation.getLat();
		lon = CurrentLocation.getLon();
		mc = mapView.getController();
	    mc.setCenter(new GeoPoint(lat,lon));
	    mc.setZoom(17);
	    
	    mapView.getOverlays().add(new CrosshairOverlay());
	
	    
	}
	
	public void onPause(){
		super.onPause();
		GeoPoint p = mapView.getMapCenter();
	    
	    
	    
	    //mapView.getProjection().toPixels(mapView.getMapCenter(), null);
    	if (p.getLatitudeE6() != CurrentLocation.getLat()){
    		System.out.println("LAT");
    		CurrentLocation.setLat(p.getLatitudeE6());
    	}		
    	if (p.getLongitudeE6() != CurrentLocation.getLon()){
    		System.out.println("LON");
    		CurrentLocation.setLon(p.getLongitudeE6());
    	}
    	
	}
	
	private class CrosshairOverlay extends Overlay {
	    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
	        Projection projection = mapView.getProjection();
	        Point center = projection.toPixels(mapView.getMapCenter(), null);

	        // Customize appearance, should be a fields.
	        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
	        p.setColor(0xFF000000);
	        p.setStyle(Style.STROKE);
	        p.setStrokeWidth(2.0f);
	        int innerRadius = 1;
	        int outerRadius = 20;
	        int crossSize = outerRadius;
	        
	        canvas.drawLine(center.x-crossSize, center.y, center.x+crossSize, center.y, p);
	        canvas.drawLine(center.x, center.y-crossSize, center.x, center.y+crossSize, p);
	        
	        canvas.drawCircle(center.x, center.y, innerRadius, p);
	        canvas.drawCircle(center.x, center.y, outerRadius, p);
	        return true;
	    }
	}
	
	
/*
	public class LocListener implements LocationListener
	{

		public void onLocationChanged(Location loc)
		{
			loc.getLatitude();
			loc.getLongitude();
			String Text = "My current location is: " +
			"Latitud = " + loc.getLatitude() +
			"Longitud = " + loc.getLongitude();
			Toast.makeText( getApplicationContext(),
			Text,
			Toast.LENGTH_SHORT).show();
		}

		public void onProviderDisabled(String provider)
		{
			Toast.makeText( getApplicationContext(),
			"Gps Disabled",
			Toast.LENGTH_SHORT ).show();
		}

		public void onProviderEnabled(String provider)
		{
			Toast.makeText( getApplicationContext(),
			"Gps Enabled",
			Toast.LENGTH_SHORT).show();
		}

		public void onStatusChanged(String provider, int status, Bundle extras)
		{
		}
*/
	
	
}
